package com.ygqh.baby.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.ao.BusinessModule;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.PayType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgOrderPaymentMapper;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.po.YgOrderPayment;
import com.ygqh.baby.po.YgOrderPaymentExample;
import com.ygqh.baby.po.YgOrderPaymentExample.Criteria;
import com.ygqh.baby.service.AbstractService;
import com.ygqh.baby.service.YgOrderPaymentService;

@Service
public class YgOrderPaymentServiceImpl extends AbstractService implements YgOrderPaymentService {

	@Autowired
	private YgOrderPaymentMapper ygOrderPaymentMapper;

	@Override
	public List<YgOrderPayment> find() {
		YgOrderPaymentExample example = new YgOrderPaymentExample();
		example.createCriteria();
		return ygOrderPaymentMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgOrderPaymentExample example = new YgOrderPaymentExample();
		example.createCriteria();
		return ygOrderPaymentMapper.countByExample(example);
	}

	@Override
	public YgOrderPayment findById(Long id) {
		return ygOrderPaymentMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgOrderPayment ygOrderPayment) {
		return ygOrderPaymentMapper.insertSelective(ygOrderPayment);
	}

	@Override
	public int update(YgOrderPayment ygOrderPayment) {

		return ygOrderPaymentMapper.updateByPrimaryKeySelective(ygOrderPayment);
	}

	@Override
	public ResultSet<YgOrderPayment> search(QueryInfo queryInfo, String q) {
		List<YgOrderPayment> list = ygOrderPaymentMapper.selectSuper(queryInfo, q);
		int count = ygOrderPaymentMapper.countSuper(q);
		ResultSet<YgOrderPayment> resultSet = new ResultSet<YgOrderPayment>(count, list);
		return resultSet;
	}

	@Override
	public int saveOrUpdate(YgOrderPayment ygOrderPayment) {
		int r = 0;
		if (ygOrderPayment.getId() != null) {
			r = this.update(ygOrderPayment);
		} else {
			r = this.save(ygOrderPayment);
		}
		return r;
	}

	@Override
	public int addPaymentRecord(BusinessModule businessModule, String orderId, BigDecimal totalPrice, String paymentCode, PayType payType, AppType appType,
			String prepayId, String platformNo, String platformName) {
		YgOrderPayment ygOrderPayment = new YgOrderPayment();
		ygOrderPayment.setOrderId(orderId.toString());
		ygOrderPayment.setBusinessModule(businessModule);
		ygOrderPayment.setOrderPayCode(paymentCode);
		ygOrderPayment.setPaidPrice(new BigDecimal(0));
		ygOrderPayment.setPrice(totalPrice);
		ygOrderPayment.setPayType(payType);
		ygOrderPayment.setAppType(appType);
		ygOrderPayment.setCreateTime(new Date());
		ygOrderPayment.setPrepayId(prepayId);
		ygOrderPayment.setPlatformNo(platformNo);
		ygOrderPayment.setPlatformName(platformName);
		if (PayType.Account.equals(payType)) {
			ygOrderPayment.setPaidPrice(ZERO);
			ygOrderPayment.setPaySuccessTime(new Date());
		}
		return this.save(ygOrderPayment);
	}

	@Override
	public int addPaymentRecord(String orderId, BigDecimal totalPrice, String paymentCode, PayType payType, AppType appType) {
		return this.addPaymentRecord(BusinessModule.Order, orderId, totalPrice, paymentCode, payType, appType, null, null, null);
	}

	@Override
	public YgOrderPayment findByPayCode(String paymentCode) {
		YgOrderPaymentExample example = new YgOrderPaymentExample();
		example.createCriteria().andOrderPayCodeEqualTo(paymentCode).andBusinessModuleEqualTo(BusinessModule.Order.name());
		List<YgOrderPayment> list = ygOrderPaymentMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public int writeBackPayment(YgOrderPayment payment) {
		YgOrderPaymentExample example = new YgOrderPaymentExample();
		example.createCriteria().andOrderPayCodeNotEqualTo(payment.getOrderPayCode()).andOrderIdEqualTo(payment.getOrderId())
		.andBusinessModuleEqualTo(BusinessModule.Order.name()).andStatusEqualTo(DataStatus.Valid.name());
		YgOrderPayment record = new YgOrderPayment();
		record.setStatus(DataStatus.Delete);
		ygOrderPaymentMapper.updateByExampleSelective(record, example);
		return ygOrderPaymentMapper.updateByPrimaryKeySelective(payment);
	}

	@Override
	public YgOrderPayment findByOrderId(String orderId) {
		 
		return this.findByOrderId(orderId, BusinessModule.Order);
	}

	@Override
	public YgOrderPayment findByOrderId(Long orderId) {
		return this.findByOrderId(orderId.toString());
	}

	@Override
	public YgOrder findOrderByPaymentCode(String paymentCode, Boolean paid) {

		return this.findOrderByPaymentCode(paymentCode, paid, BusinessModule.Order);
	}
	
	@Override
	public YgOrder findOrderByPaymentCode(String paymentCode, Boolean paid,BusinessModule businessModule) {
		
		return ygOrderPaymentMapper.selectOrderByPaymentCode(paymentCode, paid,businessModule);
	}

	@Override
	public int updatePaySuccess(String paymentCode, String tradeNo, AppType appType, PayType payType, BigDecimal paidPrice, Date paidTime) {
		return ygOrderPaymentMapper.updatePaySuccess(paymentCode, tradeNo, appType, payType, paidPrice, paidTime);
	}

	@Override
	public YgOrderPayment findByOrderId(String orderId, BusinessModule businessModule) {
		YgOrderPaymentExample example = new YgOrderPaymentExample();
		Criteria criteria = example.createCriteria().andOrderIdEqualTo(orderId);
		if(businessModule != null){
			criteria.andBusinessModuleEqualTo(businessModule.name());
		}
		criteria.andPaySuccessTimeIsNotNull().andStatusEqualTo(DataStatus.Valid.name());
		List<YgOrderPayment> list = ygOrderPaymentMapper.selectByExample(example);
		if (list != null && list.size() > 0) {

			return list.get(0);
		}
		return null;
	}
 
}
